package com.aube.views.vote;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.aube.R;
import com.aube.model.VoteInfo;
import com.aube.model.VoteItemInfo;
import com.huyn.baseframework.utils.Utils;

import java.util.concurrent.atomic.AtomicBoolean;

public class SupportView extends ViewGroup implements OnClickListener {

    private ImageButton btn_vote_red, btn_vote_blue;

    private TextView text_rate_red, text_rate_blue;

    //将内容在vs条上显示
    private TextView textRed, textBlue;

    private VoteProgressBar progressBar, leftBar, rightBar;

    private View mVoteRed, mVoteBlue;

    private float percentL = 0.5f, percentR = 0.5f;

    private int widthLeft, widthRight;

    private IVoteProxy proxy;

    private AtomicBoolean mIsAnimating = new AtomicBoolean(false);

    public void setIVoteProxy(IVoteProxy proxy) {
        this.proxy = proxy;
    }

    private static final int MIN_DISTANCE = 140;
    private int minWidth = 0;

    public SupportView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        minWidth = Utils.dip2px(context, MIN_DISTANCE);
    }

    public SupportView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SupportView(Context context) {
        this(context, null);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        for (int i = 1; i < getChildCount(); i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            height = Math.max(height, getChildAt(i).getMeasuredHeight());
        }

        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        initTextWidth();

        measureChild(progressBar, widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        measureChild(leftBar, widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
        measureChild(rightBar, widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        int x = btn_vote_red.getMeasuredWidth();
        int redRight = widthLeft - text_rate_red.getMeasuredWidth() - 10;
        measureChild(textRed, MeasureSpec.makeMeasureSpec(redRight - x, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

        int blueLeftMax = widthLeft + text_rate_blue.getMeasuredWidth() + 10;
        int blueRight = widthLeft + widthRight - btn_vote_blue.getMeasuredWidth();
        int blueLeft = blueRight - textBlue.getMeasuredWidth() > blueLeftMax ? (blueRight - textBlue.getMeasuredWidth()) : blueLeftMax;
        measureChild(textBlue, MeasureSpec.makeMeasureSpec(blueRight - blueLeft, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    private float mProgress = 0.5f;

    private void initTextWidth() {
        int widthSpace = getMeasuredWidth() - (minWidth << 1);
        widthSpace = Math.max(0, widthSpace);
        widthLeft = minWidth + (int) (widthSpace * mProgress);
        widthRight = minWidth + (int) (widthSpace * (1 - mProgress));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//        initTextWidth();
        btn_vote_red.layout(0,
                (getMeasuredHeight() - btn_vote_red.getMeasuredHeight()) / 2,
                btn_vote_red.getMeasuredWidth(),
                (getMeasuredHeight() + btn_vote_red.getMeasuredHeight()) / 2);

        text_rate_red.layout(widthLeft - text_rate_red.getMeasuredWidth(),
                (getMeasuredHeight() - text_rate_red.getMeasuredHeight()) / 2,
                widthLeft,
                (getMeasuredHeight() + text_rate_red.getMeasuredHeight()) / 2);
        text_rate_blue.layout(widthLeft, (getMeasuredHeight() - text_rate_blue.getMeasuredHeight()) / 2, widthLeft
                + text_rate_blue.getMeasuredWidth(), (getMeasuredHeight() + text_rate_blue.getMeasuredHeight()) / 2);

        btn_vote_blue.layout(widthLeft + widthRight - btn_vote_blue.getMeasuredWidth(),
                (getMeasuredHeight() - btn_vote_blue.getMeasuredHeight()) / 2,
                widthLeft + widthRight,
                (getMeasuredHeight() + btn_vote_blue.getMeasuredHeight()) / 2);
        progressBar.layout(0, (getMeasuredHeight() - progressBar.getMeasuredHeight()) / 2, progressBar.getMeasuredWidth(), (getMeasuredHeight() + progressBar.getMeasuredHeight()) / 2);
        leftBar.layout(0, (getMeasuredHeight() - leftBar.getMeasuredHeight()) / 2, leftBar.getMeasuredWidth(), (getMeasuredHeight() + leftBar.getMeasuredHeight()) / 2);
        rightBar.layout(0, (getMeasuredHeight() - rightBar.getMeasuredHeight()) / 2, rightBar.getMeasuredWidth(), (getMeasuredHeight() + rightBar.getMeasuredHeight()) / 2);

        mVoteRed.layout(0, (getMeasuredHeight() - mVoteRed.getMeasuredHeight()) / 2, widthLeft, (getMeasuredHeight() + mVoteRed.getMeasuredHeight()) / 2);
        mVoteBlue.layout(widthLeft, (getMeasuredHeight() - mVoteBlue.getMeasuredHeight()) / 2, getWidth(), (getMeasuredHeight() + mVoteBlue.getMeasuredHeight()) / 2);

        if (textRed != null) {
            int x = btn_vote_red.getMeasuredWidth();
            int y = (getMeasuredHeight() - textRed.getMeasuredHeight()) / 2;
            int redRight = widthLeft - text_rate_red.getMeasuredWidth() - 10;
            textRed.layout(x, y, redRight, y + textRed.getMeasuredHeight());
        }
        if (textBlue != null) {
            int y = (getMeasuredHeight() - textBlue.getMeasuredHeight()) / 2;
            int blueLeftMax = widthLeft + text_rate_blue.getMeasuredWidth() + 10;
            int blueRight = widthLeft + widthRight - btn_vote_blue.getMeasuredWidth();
            int blueLeft = blueRight - textBlue.getMeasuredWidth() > blueLeftMax ? (blueRight - textBlue.getMeasuredWidth()) : blueLeftMax;
            textBlue.layout(blueLeft, y, blueRight, y + textBlue.getMeasuredHeight());
        }
    }

    public void init() {
        btn_vote_red = (ImageButton) this.findViewById(R.id.btn_vote_red);
        btn_vote_blue = (ImageButton) this.findViewById(R.id.btn_vote_blue);
        text_rate_red = (TextView) this.findViewById(R.id.text_rate_red);
        text_rate_blue = (TextView) this.findViewById(R.id.text_rate_blue);
        progressBar = (VoteProgressBar) this.findViewById(R.id.progressBar);
        leftBar = (VoteProgressBar) this.findViewById(R.id.progressBarLeft);
        rightBar = (VoteProgressBar) this.findViewById(R.id.progressBarRight);
        leftBar.setLeft(true);
        rightBar.setRight(true);

        textRed = (TextView) findViewById(R.id.text_title_red);
        textBlue = (TextView) findViewById(R.id.text_title_blue);

        mVoteRed = this.findViewById(R.id.btn_vote_red_area);
        mVoteBlue = this.findViewById(R.id.btn_vote_blue_area);

        progressBar.setSpace(minWidth);
        leftBar.setSpace(minWidth);
        rightBar.setSpace(minWidth);

        progressBar.setProgressColor(getResources().getColor(R.color.vote_bg_red));
        progressBar.setBackgroundColor(getResources().getColor(R.color.vote_bg_blue));
        leftBar.setProgressColor(getResources().getColor(R.color.vote_red));
        rightBar.setProgressColor(getResources().getColor(R.color.vote_blue));
        text_rate_red.setText("50%");
        text_rate_blue.setText("50%");
    }

    private VoteInfo voteInfo;

    public void setVoteInfo(VoteInfo voteInfo) {
        setVoteInfo(voteInfo, false);
    }

    public void updateVoteInfo(VoteInfo voteInfo) {
        this.voteInfo = voteInfo;
    }

    public void setVoteInfo(VoteInfo info, boolean anim) {
        if (mIsAnimating.get())
            return;
        mIsAnimating.set(true);

        this.voteInfo = info;

        //this.voteInfo = voteInfo;
        if (voteInfo.isVoted(getContext())) {
            if (voteInfo.getVotedIndex(getContext()) == 0) {
                btn_vote_red.setImageResource(R.drawable.icon_vswhitel);
                btn_vote_red.setTag(R.id.vs_selected_state, true);
                text_rate_red.setTextColor(getResources().getColor(R.color.white));
                btn_vote_blue.setImageResource(R.drawable.icon_vsblue);
                btn_vote_blue.setTag(R.id.vs_selected_state, false);
                text_rate_blue.setTextColor(getResources().getColor(R.color.vote_blue));

                textRed.setTextColor(getResources().getColor(R.color.white));
                textBlue.setTextColor(getResources().getColor(R.color.vote_blue));
            } else {
                btn_vote_red.setImageResource(R.drawable.icon_vsred);
                btn_vote_red.setTag(R.id.vs_selected_state, false);
                text_rate_red.setTextColor(getResources().getColor(R.color.vote_red));
                btn_vote_blue.setImageResource(R.drawable.icon_vswhiter);
                btn_vote_blue.setTag(R.id.vs_selected_state, true);
                text_rate_blue.setTextColor(getResources().getColor(R.color.white));

                textRed.setTextColor(getResources().getColor(R.color.vote_red));
                textBlue.setTextColor(getResources().getColor(R.color.white));
            }
        } else {
            btn_vote_red.setImageResource(R.drawable.icon_vsred);
            btn_vote_red.setTag(R.id.vs_selected_state, false);
            text_rate_red.setTextColor(getResources().getColor(R.color.vote_red));
            btn_vote_blue.setImageResource(R.drawable.icon_vsblue);
            btn_vote_blue.setTag(R.id.vs_selected_state, false);
            text_rate_blue.setTextColor(getResources().getColor(R.color.vote_blue));

            textRed.setTextColor(getResources().getColor(R.color.vote_red));
            textBlue.setTextColor(getResources().getColor(R.color.vote_blue));
        }
//        btn_vote_red.setOnClickListener(this);
//        btn_vote_blue.setOnClickListener(this);
        mVoteRed.setOnClickListener(this);
        mVoteBlue.setOnClickListener(this);

        if (voteInfo.getContents() != null && voteInfo.getContents().size() == 2) {
            VoteItemInfo itemLeft = voteInfo.getContents().get(0);
            final int progress = (voteInfo.getTotalNum() == 0) ? 50 : Utils.calPercentInt(voteInfo.getTotalNum(), itemLeft.getNumber());
            text_rate_red.setText(progress + "%");

            text_rate_blue.setText((100 - progress) + "%");

            percentL = (voteInfo.getTotalNum() == 0) ? 0.5f : Utils.calPercentFloat(voteInfo.getTotalNum(), itemLeft.getNumber());
            percentR = 1 - percentL;

            float progressFrom = (float) progressBar.getProgress() / (float) progressBar.getMax();
            float progressTo = (float) progress / (float) progressBar.getMax();
            progressBar.setIProgressListener(new VoteProgressBar.IProgressListener() {
                @Override
                public void updateProgress(float percent) {
                    mProgress = percent;
                    requestLayout();
                }

                @Override
                public void animEnd() {
                    mIsAnimating.set(false);
                }
            });

            progressBar.setProgress(progressFrom, progressTo, anim);
            if (voteInfo.isVoted(getContext())) {
                float percent = progressTo;
                if (voteInfo.getVotedIndex(getContext()) == 0) {
                    if (leftBar.isSelected()) {
                        leftBar.setProgress(leftBar.getPercent(), percent, anim);
                        rightBar.setProgress(rightBar.getPercent(), 1f, anim);
                    } else {
                        leftBar.setProgress(0, percent, anim);
                        rightBar.setProgress(rightBar.getPercent(), 1f, anim);
                    }
                    leftBar.setSelected(true);
                    rightBar.setSelected(false);
                } else {
                    if (rightBar.isSelected()) {
                        leftBar.setProgress(leftBar.getPercent(), 0, anim);
                        rightBar.setProgress(rightBar.getPercent(), percent, anim);
                    } else {
                        leftBar.setProgress(leftBar.getPercent(), 0, anim);
                        rightBar.setProgress(1f, percent, anim);
                    }
                    leftBar.setSelected(false);
                    rightBar.setSelected(true);
                }
            } else {
                leftBar.setProgress(leftBar.getPercent(), 0, anim);
                rightBar.setProgress(rightBar.getPercent(), 1f, anim);
                leftBar.setSelected(false);
                rightBar.setSelected(false);
            }
        } else {
            progressBar.setProgress(percentL);
            leftBar.setProgress(0f);
            rightBar.setProgress(1f);
        }
    }

    public void setProgress(float progress) {
        percentL = progress;
        percentR = 1 - percentL;
        progressBar.setProgress(progress);
        requestLayout();
    }

    @Override
    public void onClick(View v) {
        if (mIsAnimating.get())
            return;
        if (v == mVoteRed)
            proxy.onClickListener(btn_vote_red, 0);
        else
            proxy.onClickListener(btn_vote_blue, 1);
    }

}
